package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.platform.enhance.repository.CustomerRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.domain.customer.dto.BatchQueryRequest;
import com.epoch.app.crplatformenhance.domain.customer.dto.BatchQueryResponse;
import com.epoch.app.crplatformenhance.domain.customer.dto.CustomerLoadListRequest;
import com.epoch.app.crplatformenhance.domain.customer.dto.LoadCustomerRequest;
import com.epoch.app.crplatformenhance.domain.customer.model.Customer;
import com.epoch.app.crplatformenhance.domain.customer.service.CustomerService;
import com.epoch.app.crplatformenhance.dto.request.CustomerBatchRequest;
import com.epoch.app.crplatformenhance.dto.request.CustomerRequest;
import com.epoch.app.crplatformenhance.dto.request.QueryCustomerRequest;
import com.epoch.app.crplatformenhance.sdo.CustomerSDO;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class CustomerRepositoryImpl implements CustomerRepository {

    @Resource
    CustomerService customerService;

    @Override
    @RepositoryInvoker
    public List<CustomerSDO> batchQuery(CustomerBatchRequest request) {
        BatchQueryRequest batchQueryRequest = new BatchQueryRequest();
        BeanUtils.copyProperties(request, batchQueryRequest);
        batchQueryRequest.setLimit(1000);
        BatchQueryResponse batchQueryResponse = customerService.batchQuery(batchQueryRequest);
        if (CollectionUtils.isEmpty(batchQueryResponse.getResult())) {
            return Lists.newArrayList();
        }
        List<CustomerSDO> collect = batchQueryResponse.getResult().stream().map(it -> {
            CustomerSDO customerSDO = new CustomerSDO();
            BeanUtils.copyProperties(it, customerSDO);
            return customerSDO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    @RepositoryInvoker
    public PageResult<List<CustomerSDO>> pageQuery(QueryCustomerRequest request) {
        CustomerLoadListRequest loadListRequest = new CustomerLoadListRequest();
        BeanUtils.copyProperties(request, loadListRequest);
        loadListRequest.setDir("gmt_create");
        Result<List<Customer>> customerResult = customerService.loadCustomerList(loadListRequest);
        List<CustomerSDO> resultList = new ArrayList<>();
        for (Customer customer : customerResult.getResult()) {
            CustomerSDO customerSDO = new CustomerSDO();
            BeanUtils.copyProperties(customer, customerSDO);
            fillInProvinceAndCityAndArea(customer, customerSDO);
            customerSDO.setId(customer.getId().toString());
            resultList.add(customerSDO);
        }
        return PageResult.of(resultList, customerResult.getTotal());

    }

    private void fillInProvinceAndCityAndArea(Customer customer, CustomerSDO customerSDO) {
        if (Objects.nonNull(customer.getFeatures())) {
            JSONObject jsonObject = JSONObject.parseObject(customer.getFeatures());
            if (Objects.nonNull(jsonObject)) {
                if (Objects.nonNull(jsonObject.get("registerProvinceName"))) {
                    String registerProvinceName = (String) jsonObject.get("registerProvinceName");
                    customerSDO.setRegisterProvinceName(registerProvinceName);
                }
                if (Objects.nonNull(jsonObject.get("registerCityName"))) {
                    String registerCityName = (String) jsonObject.get("registerCityName");
                    customerSDO.setRegisterCityName(registerCityName);

                }
                if (Objects.nonNull(jsonObject.get("registerRegioName"))) {
                    String regioName = (String) jsonObject.get("registerRegioName");
                    customerSDO.setRegioName(regioName);

                }
            }
        }
    }

    @Override
    @RepositoryInvoker
    public CustomerSDO query(CustomerRequest customerRequest) {
        LoadCustomerRequest loadRequest = new LoadCustomerRequest();
        BeanUtils.copyProperties(customerRequest, loadRequest);
        Customer customer = customerService.loadCustomer(loadRequest);
        CustomerSDO sdo = new CustomerSDO();
        BeanUtils.copyProperties(customer, sdo);
        if(StringUtils.isNotEmpty(customer.getFeatures())){
            JSONObject jsonObject = JSONObject.parseObject(customer.getFeatures());
            String registerProvinceName = jsonObject.getString("registerProvinceName");
            sdo.setRegisterProvinceName(registerProvinceName);
            String registerCityName = jsonObject.getString("registerCityName");
            sdo.setRegisterCityName(registerCityName);
            String registerRegioName = jsonObject.getString("registerRegioName");
            sdo.setRegioName(registerRegioName);
        }
        return sdo;
    }

}
